<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>装饰器模式的弹框</title>
  <style>
    #modal {
      width: 200px;
      height: 200px;
      line-height: 200px;
      position: fixed;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #000;
      text-align: center;
    }
  </style>
</head>

<body>
  <button id="open">打开弹框</button>
  <button id="close">关闭弹框</button>

  <!-- <script>
    const Modal = (function() {
      let modal = null
      return function() {
        if (!modal) {
          modal = document.createElement('div')
          modal.innerHTML = '您还未登录哦~'
          modal.id = 'modal'
          modal.style.display = 'none'
          document.body.appendChild(modal)
        }
        return modal
      }
    })()

    function openModal() {
      const modal = new Modal()
      modal.style.display = 'block'
    } 

    document.getElementById('open').addEventListener('click', function() {
      openModal()
      changeButtonStatus()
    })

    document.getElementById('close').addEventListener('click', function() {
      const modal = new Modal()
      if (modal) {
        modal.style.display = 'none'
      }
    })

    // 新的逻辑
    function changeButtonText() {
      const btn = document.getElementById('open')
      btn.innerText = '快去登录'
    }
    function disableButton() {
      const btn = document.getElementById('open')
      btn.setAttribute('disabled', true)
    }
    function changeButtonStatus() {
      changeButtonText()
      disableButton()
    }
  </script> -->

  <script>
    const Modal = (function () {
      let modal = null
      return function () {
        if (!modal) {
          modal = document.createElement('div')
          modal.innerHTML = '您还未登录哦~'
          modal.id = 'modal'
          modal.style.display = 'none'
          document.body.appendChild(modal)
        }
        return modal
      }
    })()


    class OpenButton {
      onClick() {
        const modal = new Modal()
        modal.style.display = 'block'
      }
    }

    // 装饰器
    class Decorator {
      constructor(open_button) {
        this.open_button = open_button
      }

      onClick() {
        this.open_button.onClick()
        this.changeButtonStatus()
      }

      changeButtonStatus() {
        this.changeButtonText()
        this.disableButton()
      }

      changeButtonText() {
        const btn = document.getElementById('open')
        btn.innerText = '快去登录'
      }
      disableButton() {
        const btn = document.getElementById('open')
        btn.setAttribute('disabled', true)
      }

    }

    const openButton = new OpenButton()
    const decorator = new Decorator(openButton)

    document.getElementById('open').addEventListener('click', function () {
      // openButton.onClick()
      decorator.onClick()
    })
  </script>
</body>

</html>